home *** CD-ROM | disk | FTP | other *** search
- /*LINTLIBRARY*/
-
- /* @(#)events.c 1.18 91/11/13
- *
- * Procedures for handling various events in reve.
- *
- * Copyright (C) 1990, 1991 - Rich Burridge & Yves Gallot.
- * All rights reserved.
- *
- * Permission is granted to copy this source, for redistribution
- * in source form only, provided the news headers in "substantially
- * unaltered format" are retained, the introductory messages are not
- * removed, and no monies are exchanged.
- *
- * Permission is also granted to copy this source, without the
- * news headers, for the purposes of making an executable copy by
- * means of compilation, provided that such copy will not be used
- * for the purposes of competition in any othello tournaments, without
- * prior permission from the authors.
- *
- * No responsibility is taken for any errors on inaccuracies inherent
- * either to the comments or the code of this program, but if reported
- * (see README file), then an attempt will be made to fix them.
- */
-
- #include "reve.h"
- #include "color.h"
- #include "extern.h"
-
-
- void
- check_button_down(item)
- enum panel_type item ;
- {
- int n ;
-
- n = (int) item ;
- if (items[n].x == -1) return ;
- if (items[n].wtype != curwin) return ;
- if (tinput && (n == (int) SUGGEST_BUT || n == (int) PRINT_BUT ||
- n == (int) STOP_BUT || n == (int) UNDO_BUT))
- return ;
- if ((curx > items[n].x) && (curx < (items[n].x + items[n].width)) &&
- (cury > items[n].y) && (cury < (items[n].y + items[n].height)))
- {
- down = nextc ;
- itemno = n ;
- but_inverted = itemno ;
- draw_button(W_PANEL, (enum panel_type) itemno, C_ITEMS, BUT_INVERT) ;
- }
- }
-
-
- void
- check_choice_down(item)
- enum panel_type item ;
- {
- int n ;
-
- n = (int) item ;
- if (items[n].wtype != curwin) return ;
- if ((curx > items[n].x) && (curx < (items[n].x + items[n].width)) &&
- (cury > items[n].y) && (cury < (items[n].y + items[n].height)))
- {
- down = nextc ;
- itemno = n ;
- }
- }
-
-
- void
- check_cycle_down(item)
- enum panel_type item ;
- {
- int ix, n ;
-
- n = (int) item ;
- if (items[n].wtype != curwin) return ;
- ix = items[n].x + 40 ;
- if ((curx > ix) && (curx < (ix + (CWIDTH / 2))) &&
- (cury > items[n].y) && (cury < (items[n].y + CHEIGHT)) &&
- nextc != RIGHT_DOWN)
- {
- direction = INCREMENT ;
- down = nextc ;
- itemno = n ;
- draw_cycle(curwin, (enum panel_type) n, C_ITEMS, CY_LINVERT) ;
- }
- else if ((curx > ix) && (curx < (ix + CWIDTH)) &&
- (cury > items[n].y) && (cury < (items[n].y + CHEIGHT)) &&
- nextc != RIGHT_DOWN)
- {
- direction = DECREMENT ;
- down = nextc ;
- itemno = n ;
- draw_cycle(curwin, (enum panel_type) n, C_ITEMS, CY_RINVERT) ;
- }
- }
-
-
- void
- check_item_down()
- {
- int n ;
-
- for (n = 0; n < MAXITEMS; n++)
- switch (items[n].type)
- {
- case P_BUTTON : check_button_down((enum panel_type) n) ;
- break ;
- case P_CHOICE : check_choice_down((enum panel_type) n) ;
- break ;
- case P_CYCLE : check_cycle_down((enum panel_type) n) ;
- break ;
- case P_MESSAGE : break ; /* Do nothing. */
- case P_TOGGLE : check_toggle_down((enum panel_type) n) ;
- }
- }
-
-
- void
- check_item_up()
- {
- if ((nextc == LEFT_UP && down == LEFT_DOWN) ||
- (nextc == MIDDLE_UP && down == MIDDLE_DOWN) ||
- (nextc == RIGHT_UP && down == RIGHT_DOWN))
- {
- if (items[itemno].type == P_BUTTON && but_inverted == -1) return ;
- handle_item(items[itemno].value) ;
-
- switch (items[itemno].type)
- {
- case P_BUTTON : if (items[itemno].x != -1)
- draw_button(items[itemno].wtype,
- (enum panel_type) itemno,
- W_COLOR(C_ITEMS), BUT_NORMAL) ;
- break ;
- case P_CHOICE : draw_choice(items[itemno].wtype,
- (enum panel_type) itemno,
- W_COLOR(C_PANEL)) ;
- break ;
- case P_CYCLE : draw_cycle(items[itemno].wtype,
- (enum panel_type) itemno,
- W_COLOR(C_ITEMS), CY_NORMAL) ;
- break ;
- case P_TOGGLE : draw_toggle(items[itemno].wtype,
- (enum panel_type) itemno,
- W_COLOR(C_PANEL)) ;
- }
- }
- }
-
-
- void
- check_toggle_down(item)
- enum panel_type item ;
- {
- int n ;
-
- n = (int) item ;
- if (items[n].wtype != curwin) return ;
- if ((curx > items[n].x) && (curx < (items[n].x + items[n].width)) &&
- (cury > items[n].y) && (cury < (items[n].y + items[n].height)))
- {
- down = nextc ;
- itemno = n ;
- }
- }
-
-
- void
- do_action()
- {
- int cx, cy, thismove ;
-
- switch (nextc)
- {
- case MOUSE_MOVING : if (ANIMATION)
- draw_piece(next_player, piece_x, piece_y, RINV) ;
- piece_x = curx - pieceXrad ;
- piece_y = cury - pieceYrad ;
-
- cx = (piece_x + pieceXrad - bborder) / cell_width ;
- cy = (piece_y + pieceYrad - bborder) / cell_height ;
-
- if (cx >= 0 && cy >= 0 && cx < 8 && cy < 8)
- {
- thismove = cy * BOARD_SIZE + cx ;
-
- if (thismove != last_outline)
- draw_outline(last_outline, IS_OFF) ;
- last_outline = thismove ;
- draw_outline(last_outline, IS_ON) ;
- }
-
- if (ANIMATION)
- draw_piece(next_player, piece_x, piece_y, RINV) ;
- break ;
-
- case ENTER_WINDOW :
- case EXIT_WINDOW : set_cursor(CANVASCUR) ;
- draw_outline(last_outline, IS_OFF) ;
- last_outline = -1 ;
- if (ANIMATION)
- draw_piece(next_player, piece_x, piece_y, RINV) ;
- cmode = (enum cantype) ((int) cmode - 1) ;
- break ;
-
- case LEFT_DOWN :
- case LEFT_UP :
- case MIDDLE_DOWN :
- case MIDDLE_UP :
- case RIGHT_DOWN :
- case RIGHT_UP : set_cursor(CANVASCUR) ;
- do_selection(nextc) ;
- }
- }
-
-
- void
- do_key(item, ch)
- int item, ch ;
- {
- int color, maxw, val ;
-
- color = W_COLOR(C_PANEL) ;
- if (!validkey)
- {
- validkey = cur_ch ;
- switch (item)
- {
- case 'C' : message(PANEL_MES, items[(int) COMP_CHOICE].label) ;
- break ;
- case 'D' : message(PANEL_MES, items[(int) DIFF_CHOICE].label) ;
- break ;
- case 'O' : message(PANEL_MES, "Option?") ;
- }
- }
- else
- {
- val = -1 ;
- switch (item)
- {
- case 'C' : itemno = (int) COMP_CHOICE ;
- switch (ch)
- {
- case 'a' : /* All (both). */
- case 'A' : val = CP_BOTH ;
- break ;
- case 'b' : /* Black. */
- case 'B' : val = CP_BLACK ;
- break ;
- case 'n' : /* Neither. */
- case 'N' : val = CP_NEITHER ;
- break ;
- case 'w' : /* White. */
- case 'W' : val = CP_WHITE ;
- break ;
- default : return ;
- }
- maxw = items[itemno].width / items[itemno].nopts ;
- curx = items[itemno].x + (maxw * val) ;
- items[itemno].value = val ;
- break ;
-
- case 'D' : itemno = (int) DIFF_CHOICE ;
- if (ch >= '1' && ch <= '9') val = ch - '1' ;
- maxw = items[itemno].width / items[itemno].nopts ;
- curx = items[itemno].x + (maxw * val) ;
- items[itemno].value = val ;
- break ;
-
- case 'O' : switch (ch)
- {
- case 'a' :
- case 'A' : itemno = (int) OPT_ANIM ;
- break ;
- case 'b' :
- case 'B' : itemno = (int) OPT_BEST ;
- break ;
- case 'c' :
- case 'C' : itemno = (int) OPT_CLK ;
- break ;
- case 'l' :
- case 'L' : itemno = (int) OPT_LAST ;
- break ;
- case 'e' :
- case 'E' : itemno = (int) OPT_EVAL ;
- break ;
- case 'm' :
- case 'M' : itemno = (int) OPT_MOVE ;
- break ;
- case 'n' :
- case 'N' : itemno = (int) OPT_NUM ;
- break ;
- case 'f' :
- case 'F' : itemno = (int) OPT_FLIP ;
- break ;
- default : return ;
- }
- val = items[itemno].value ;
- }
- if (val != -1)
- {
- validkey = 0 ;
- message(PANEL_MES, "") ;
- items[itemno].value = item_value = val ;
- direction = NONE ;
- (*items[itemno].func)() ;
-
- switch (item)
- {
- case 'C' : draw_choice(W_PROPS, (enum panel_type) itemno, color) ;
- break ;
- case 'D' : draw_choice(W_PROPS, (enum panel_type) itemno, color) ;
- break ;
- case 'O' : draw_toggle(W_PROPS, (enum panel_type) itemno, color) ;
- }
- }
- }
- }
-
-
- void
- do_key_move(n1, n2)
- int n1, n2 ;
- {
- move = (n2 - '1') * BOARD_SIZE + (n1 - 'a') ;
- next_player = (int) cmode - 1 ;
- cmode = (enum cantype) ((int) cmode + 1) ;
- update_clock(next_player, TRUE) ;
- make_move() ;
- reset_clock(next_player) ;
- validkey = 0 ;
- }
-
-
- void
- get_xy(n, x, y) /* Return piece coordinates given board index. */
- int n, *x, *y ;
- {
- *x = (n & 7) * cell_width + bborder + pieceXmargin ;
- *y = (n >> 3) * cell_height + bborder + pieceYmargin ;
- }
-
-
- void
- handle_board_event()
- {
- switch (cmode)
- {
- case WHITE_START :
- case BLACK_START : next_player = (int) cmode - 1 ;
- if (nextc == LEFT_DOWN || nextc == MIDDLE_DOWN ||
- nextc == RIGHT_DOWN)
- {
- set_cursor(NOCURSOR) ;
- piece_x = curx - pieceXrad ;
- piece_y = cury - pieceYrad ;
- if (ANIMATION)
- draw_piece(next_player,
- piece_x, piece_y, RINV) ;
- cmode = (enum cantype) ((int) cmode + 1) ;
- }
- break ;
- case WHITE_MOVING :
- case BLACK_MOVING : do_action() ;
- }
- }
-
-
- void
- handle_event()
- {
- process_event() ;
-
- if (nextc == BOARD_REPAINT) paint_board() ;
- else if (nextc == PANEL_REPAINT) paint_panel() ;
- else if (nextc == HELP_REPAINT) paint_help() ;
- else if (nextc == PROPS_REPAINT) paint_prop_sheet() ;
- else
- {
- if (processing == TRUE)
- {
- if (nextc == MOUSE_MOVING || nextc == IGNORE_EVENT ||
- nextc == ENTER_WINDOW || nextc == EXIT_WINDOW) return ;
- else
- {
- message(PANEL_MES, "It's not your turn.") ;
- beep() ;
- }
- return ;
- }
-
- if (nextc == KEYBOARD) handle_key() ;
- else if (nextc == EXIT_WINDOW && but_inverted != -1)
- {
- draw_button(W_PANEL, (enum panel_type) but_inverted,
- C_ITEMS, BUT_NORMAL) ;
- but_inverted = -1 ;
- down = 0 ;
- }
- else if (curwin == W_BOARD) handle_board_event() ;
- else if (nextc == LEFT_UP || nextc == MIDDLE_UP || nextc == RIGHT_UP)
- check_item_up() ;
- else if (nextc == LEFT_DOWN ||
- nextc == MIDDLE_DOWN || nextc == RIGHT_DOWN)
- check_item_down() ;
- }
- }
-
-
- void
- handle_item(val)
- int val ;
- {
- items[itemno].value = item_value = val ;
- (*items[itemno].func)() ;
- but_inverted = -1 ;
- down = 0 ;
- }
-
-
- void
- handle_key() /* Process the latest key that the user has pressed. */
- {
- char str[9] ; /* To display half move position. */
- int nextc ;
-
- if (tinput)
- {
- get_filename() ;
- return ;
- }
- if (cur_ch == ESCAPE) validkey = 0 ;
- if (validkey)
- {
- nextc = cur_ch ;
- cur_ch = validkey ;
- }
- switch (cur_ch)
- {
- case 'C' : /* Computer choice. */
- case 'D' : /* Difficulty. */
- case 'O' : do_key(cur_ch, nextc) ; /* Options. */
- break ;
-
- case 'H' : do_help() ;
- break ;
- case 'M' : show_all_moves() ; /* Button items. */
- break ;
- case 'L' : curx = 0 ;
- draw_textfield() ;
- break ;
- case 'P' : print_game() ;
- break ;
- case 'n' : new_game() ;
- break ;
- case 'r' : redo() ;
- break ;
- case 'S' : curx = BBORDER + (2*(BWIDTH+BGAP)) ;
- draw_textfield() ;
- break ;
- case 's' : suggest() ;
- break ;
- case 'u' : undo() ;
- break ;
- case 'p' : do_props() ;
- break ;
-
- case CTL('l') : paint_all() ;
- break ;
-
- case 'q' : destroy_reve() ;
- exit(0) ;
-
- case '1' :
- case '2' :
- case '3' :
- case '4' :
- case '5' :
- case '6' :
- case '7' :
- case '8' : if (!validkey)
- {
- validkey = cur_ch ;
- SPRINTF(str, "Move: %c", cur_ch) ;
- message(PANEL_MES, str) ;
- }
- else if (nextc >= 'a' && nextc <= 'h')
- {
- SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
- message(PANEL_MES, str) ;
- do_key_move(nextc, cur_ch) ;
- }
- else validkey = 0 ;
- break ;
- case 'a' :
- case 'b' :
- case 'c' :
- case 'd' :
- case 'e' :
- case 'f' :
- case 'g' :
- case 'h' : if (!validkey)
- {
- validkey = cur_ch ;
- SPRINTF(str, "Move: %c", cur_ch) ;
- message(PANEL_MES, str) ;
- }
- else if (nextc >= '1' && nextc <= '8')
- {
- SPRINTF(str, "Move: %c%c", cur_ch, nextc) ;
- message(PANEL_MES, str) ;
- do_key_move(cur_ch, nextc) ;
- }
- else validkey = 0 ;
- break ;
- default : message(PANEL_MES, "") ;
- validkey = 0 ;
- }
- }
-